package com.example.tbplus14.Test;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StopWatch;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;

public class ThreadTest {

    public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("编排start");
        List<CompletableFuture<String>> list = new ArrayList<>();
        stopWatch.stop();
        stopWatch.start("编排forstart");
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                return getStringStr();
            }, Executors.newFixedThreadPool(5));
            list.add(future);
        }
        stopWatch.stop();
        stopWatch.start("编排allOfstart");
        CompletableFuture.allOf(list.toArray(new CompletableFuture[0])).join();
        stopWatch.stop();
        stopWatch.start("编排allOfforstart");
        for (CompletableFuture<String> future : list) {
            String join = future.join();
            System.out.println(join);
        }
        stopWatch.stop();
        System.out.println(stopWatch.prettyPrint());


    }

    private static String getStringStr(){
        return System.currentTimeMillis() + "";
    }


}
